/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/
/* Includes ----------------------------------------------------------------------------*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Bsp.h"
#include "device_pins.h"
#include "button.h"
#include "PowerManager.h"
#include "button.h"
#include "main.h"
#include "UI.h"
#include "UI_PreStart.h"
#include "oled.h" 	

#include "O2Sensor.h"
/* Extern variables --------------------------------------------------------------------*/
/* Private define ----------------------------------------------------------------------*/
/* Private macro -----------------------------------------------------------------------*/
/* Private typedef ---------------------------------------------------------------------*/
/* Private variables -------------------------------------------------------------------*/
static TimerHandle_t ui_timer = NULL;
static StaticTimer_t xui_timer;
static EVENT_SRCEEN event_screen;
static void cb_ui_timer(TimerHandle_t parameter);
static SemaphoreHandle_t mtx_process_event = NULL;     // 互斥量句柄
static StaticSemaphore_t xmtx_process_event;  // 存储互斥量的数据结构

static int32_t cond_finished;
/* Private functions -------------------------------------------------------------------*/
static HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev);
static HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters);
static HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID);
static HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID);

static HMI_SCREEN_ACTION	UI_action = 
{
	UI_Initialize,
	UI_Prepare,
	UI_Refresh,
	UI_ProcessEvent,
	UI_PostProcess,
	UI_ExitScreen,
};

HMI_SCREEN_OBJECT		UI_PreStart = 
{
	UI_SCREEN_ID_PreStart,
	&UI_action
};

/*****************************************************************************************
* Function Name  : UI_Welcome_Initialize
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Initialize(SGUI_SCR_DEV *pDev)
{	
	cond_finished = -10;
    
	mtx_process_event = xSemaphoreCreateMutexStatic(&xmtx_process_event);
	
	ui_timer = xTimerCreateStatic("tm_ui_period", pdMS_TO_TICKS(80), pdTRUE, NULL, cb_ui_timer, &xui_timer);
	
	HMI_EVENT_INIT(event_screen);
	
	return HMI_RET_NORMAL;
}

extern void width_set_point(int32_t width);
/*****************************************************************************************
* Function Name  : UI_ExitScreen
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ExitScreen(SGUI_SCR_DEV *pDev)
{	
	xTimerStop(ui_timer, portMAX_DELAY);
	
#if DEV_TYPE == DEV_TYPE_I7
//	OLED_Clear();
//	OLED_Refresh();
#elif DEV_TYPE == DEV_TYPE_XP
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
#elif DEV_TYPE == DEV_TYPE_MINI
//	rses_led_control(&led_sys_r,0);
//	rses_led_control(&led_sys_g,0);
#elif DEV_TYPE == DEV_TYPE_VACUUM

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

#elif DEV_TYPE == DEV_TYPE_RPT_STD
      
#else
#error
#endif	
	vTaskDelay(pdMS_TO_TICKS(100));
	return HMI_RET_NORMAL;
}


static uint8_t cond_time_instatus;
static uint8_t cond_routine;


static uint8_t bat_timer;
static uint8_t end_timer;


static uint16_t routine;
static uint16_t routine_instatus;
static uint32_t routine_timer;

int p_start_mode;
/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Prepare(SGUI_SCR_DEV *pDev,const void* pParameters)
{			
	p_start_mode = (int)pParameters;
	      
	cond_finished = -10;
    
    routine = 0;
	routine_instatus = 0;
    routine_timer = 0;
    
    if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
		return HMI_RET_NORMAL;
	}

	if(iProfile.temperature.ColdTemper>=(iProfile.conf.start_max_temp*10))
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
		return HMI_RET_NORMAL;
	}
    		  
    meas_params_copy(&iProfile.meas_params,&iProfile.meas_params_cur);
    
#if DEV_TYPE == DEV_TYPE_I7 
	
#elif DEV_TYPE == DEV_TYPE_XP  
	
#elif DEV_TYPE == DEV_TYPE_MINI
    
#elif DEV_TYPE == DEV_TYPE_VACUUM

#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

	rt_timer_start(&ui_timer);
    
#elif DEV_TYPE == DEV_TYPE_RPT_STD 
    
#else
#error
#endif
	
	iProfile.cmd_ctrl.Status = COMMAND_CTRL_STS_PREPARE;
    
	return HMI_RET_NORMAL;
}
//


static void Cond_disp(void)
{

#if DEV_TYPE == DEV_TYPE_I7
    
    
#elif DEV_TYPE == DEV_TYPE_XP
    
#elif DEV_TYPE == DEV_TYPE_MINI

#elif DEV_TYPE == DEV_TYPE_VACUUM
    
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
    
    if((iProfile.cmd_ctrl.b_meas_select & MEAS_SELECT_OXYGEN)!=0)
    {
        rses_led_control(&led_ble,LED_COLOR_BLUE);
    }
    else
    {
        rses_led_control(&led_ble,LED_COLOR_BLACK);
    }

#elif DEV_TYPE == DEV_TYPE_RPT_STD
    
#else 
#error
#endif    
}


/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_Refresh(SGUI_SCR_DEV *pDev,const void* pParameters)
{
	//uint8_t temp_cond_timer;
	int temp_i32;
    
	if(cond_finished == -1)
	{
		HMI_SwitchScreen(UI_SCREEN_ID_Main_Parge,NULL);
		return HMI_RET_NORMAL;
	}
    else  if(cond_finished >= 0)
	{
		HMI_SwitchScreen(UI_SCREEN_ID_CondStart,NULL);
		return HMI_RET_NORMAL;
	}
	
	routine_timer++;
	
	if((iProfile.sys_status.BatChargeStatus == 0)&&(iProfile.sys_status.BatLevelStatus < 5)&&(iProfile.sys_status.BatVoltage < 3400))
	{
		bat_timer++;
	}else
	{
		bat_timer = 0;
	}
	
	if(bat_timer > 100)	//5s
	{
		cond_finished = -1;
		return HMI_RET_NORMAL;
	}
	
		
#if (DEV_TYPE == DEV_TYPE_I7) ||(DEV_TYPE == DEV_TYPE_XP)||(DEV_TYPE == DEV_TYPE_MINI)||(DEV_TYPE == DEV_TYPE_VACUUM)||(DEV_TYPE == DEV_TYPE_RPT_STD)
	
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

    if(iProfile.cmd_ctrl.Meas_ctrl == COMMAND_CTRL_STOP)
	{
        oxy_sensor_stop();
		iProfile.cmd_ctrl.Meas_ctrl = COMMAND_CTRL_NONE;
		cond_finished = -1;
		return HMI_RET_NORMAL;
	}
    
    
    switch(routine)
    {
        case 0:
        {
            oxy_sensor_start();
		
            routine_timer=0;
            routine_instatus=1;
            
            routine = 1;
            
        }break;
        
        case 1:
        {
            if(routine_timer > 10)
            {
                temp_i32 = oxy_sensor_status();
                if(temp_i32 != 0)
                {
                    routine_timer=0;
                    routine_instatus=1;
            
                    routine = 2;
                }
            }
        }break;
        
        case 2:
        {
            temp_i32 = oxy_sensor_status();
            
            if(temp_i32>10)
			{
                routine_timer=0;
                routine_instatus=0;

                routine = 5;
			}	
            
            if((routine_timer%2) == 0)
            {
                rses_led_control(&led_run,LED_COLOR_BLUE); 
            }
            else
            {
                rses_led_control(&led_run,LED_COLOR_BLACK);
            }                            
        }break;
        
        case 5:
        {
            cond_finished = MEAS_COND_START_MANUAL;
            
            
            routine_timer=0;
            routine_instatus=0;

            routine = 6;
            
            return HMI_RET_NORMAL;
        }break;
        
        case 6:
        {
            
        }break;
        
        default:
        {
            routine = 0;
            routine_instatus = 0;
            routine_timer = 0;
        }break;
    }
    
#else
#error
#endif
		
    Cond_disp();
    
	iProfile.sys_status.CondTimer++;
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : 
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
HMI_ENGINE_RESULT	UI_ProcessEvent(SGUI_SCR_DEV *pDev,const HMI_EVENT_BASE* pEvent,SGUI_INT* pActionID)
{
	HMI_ENGINE_RESULT		eProcessResult;
	SGUI_UINT16				KeyValue;
	SGUI_UINT16				KeyAction;
	KEY_PRESS_EVENT			*pKeyEvent;
	SGUI_INT				ProcessAction;
	
	xSemaphoreTake(mtx_process_event, portMAX_DELAY);
	
	eProcessResult = HMI_RET_NORMAL;
	ProcessAction = HMI_DEMO_PROC_NO_ACT;
	
	if(EVENT_TYPE_ACTION == pEvent->iType)
	{
		if(EVENT_ID_KEY_PRESS == pEvent->iID)
		{			
			pKeyEvent = (KEY_PRESS_EVENT*)pEvent;
			KeyValue = EVENT_KEY_ID(pKeyEvent->Data.uiKeyValue);
			KeyAction = EVENT_KEY_ACTION(pKeyEvent->Data.uiKeyValue);	
			
			switch(KeyValue)
			{
				case KEY_PW:
				{				
											
#if DEV_TYPE == DEV_TYPE_I7		
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
						eProcessResult = HMI_RET_CANCEL_KEY;
						cond_finished = -1;	
                    }
                        
#elif DEV_TYPE == DEV_TYPE_XP
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
                        rses_led_control(&led_run,LED_COLOR_BLACK);
                        cond_finished = -1;	
                    }
                    
#elif DEV_TYPE == DEV_TYPE_MINI	
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
                        rses_led_control(&led_run,LED_COLOR_BLACK);
                        cond_finished = -1;	
                    }
#elif DEV_TYPE == DEV_TYPE_VACUUM
                    
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_RED);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
                        rses_led_control(&led_run,LED_COLOR_BLACK);
                        cond_finished = -1;
                    }
                    
                    if(KeyAction == BUTTON_EVENT_DOUBLE)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
                        
                        rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);
                        
                        cond_finished = MEAS_COND_START_MANUAL;
                    }
    
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD
                    
                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
						oxy_sensor_stop();
						cond_finished = -1;	
                    }
                    
                    
#elif DEV_TYPE == DEV_TYPE_RPT_STD		

					if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
						cond_finished = -1;	
                        
                    }
                        
#else
#error
#endif		

				}break;
				
				case KEY_UP:
				{		
					if(KeyAction == BUTTON_EVENT_LONG)
					{	
						eProcessResult = HMI_RET_CANCEL_KEY;
#if DEV_TYPE == DEV_TYPE_I7							
						cond_finished = MEAS_COND_START_MANUAL;
#elif DEV_TYPE == DEV_TYPE_XP							
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);
                        
                        cond_finished = MEAS_COND_START_MANUAL;
                        
#elif DEV_TYPE == DEV_TYPE_MINI	
						
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);					
						rses_led_control(&led_run,LED_COLOR_GREEN);
						rt_thread_delay(80);
						rses_led_control(&led_run,LED_COLOR_BLACK);
						rt_thread_delay(80);
                        
                        cond_finished = MEAS_COND_START_MANUAL;
                        
#elif DEV_TYPE == DEV_TYPE_VACUUM					
                        
#elif DEV_TYPE == DEV_TYPE_OXYGEN_STD

                    if(KeyAction == BUTTON_EVENT_LONG)
					{
                        eProcessResult = HMI_RET_CANCEL_KEY;
						oxy_sensor_stop();
						cond_finished = -1;	
                    }

#elif DEV_TYPE == DEV_TYPE_RPT_STD							
						cond_finished = MEAS_COND_START_MANUAL;		
#else
#error
#endif																			
					}
				}break;

                case KEY_DOWN:
				{		
					eProcessResult = HMI_RET_CANCEL_KEY;  
                }break;           
			}
		}
		else if(EVENT_ID_SCREEN == pEvent->iID)
		{
			EVENT_SRCEEN *temp_event;
			temp_event = (EVENT_SRCEEN*)pEvent;
			ProcessAction = HMI_DEMO_PROC_NO_ACT;
						
			if(temp_event->Data == 1)
			{
				UI_Refresh(pDev,NULL);
			}
		}
	}
	else if(EVENT_TYPE_DATA == pEvent->iType)
	{
	}
	
	if(pActionID != NULL)
	{
		*pActionID = ProcessAction;
	}
	
	 xSemaphoreGive(mtx_process_event);
	
	return eProcessResult;
}

HMI_ENGINE_RESULT	UI_PostProcess(SGUI_SCR_DEV *pDev,HMI_ENGINE_RESULT eProcResult,SGUI_INT ActionID)
{
	if(HMI_PROCESS_SUCCESSFUL(eProcResult))
	{
		
	}
	return HMI_RET_NORMAL;
}
//

/*****************************************************************************************
* Function Name  : cb_timer_autoback
* Description    : 
* Input          : None
* Output         : None
* Return         : None
*****************************************************************************************/
static void cb_ui_timer(TimerHandle_t parameter)
{
	event_screen.Head.iType =	EVENT_TYPE_ACTION;
	event_screen.Head.iID 	=	EVENT_ID_SCREEN;
	event_screen.Data = 1;	
	HMI_ProcessEvent((HMI_EVENT_BASE*)&event_screen);
}
//

/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/

